package com.xunlai.infra.permission.manager.web.service.impl;

import com.xunlai.infra.permission.business.dao.entity.Member;
import com.xunlai.infra.permission.business.dao.repository.MemberRepository;
import com.xunlai.infra.permission.common.exception.AccountAlreadyExistsException;
import com.xunlai.infra.permission.common.exception.DataNotExistException;
import com.xunlai.infra.permission.common.exception.ExceptionCodeEnum;
import com.xunlai.infra.permission.common.exception.PermissionBusinessException;
import com.xunlai.infra.permission.manager.web.service.UserMemberService;
import com.xunlai.infra.permission.manager.web.vo.login.RegisterReqVo;
import com.xunlai.infra.permission.storage.entity.UserAccount;
import com.xunlai.infra.permission.storage.entity.UserInfo;
import com.xunlai.infra.permission.storage.repository.UserAccountRepository;
import com.xunlai.infra.permission.storage.repository.UserInfoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * @author liang
 * @date 9/5/23 3:19 PM
 */
@Service
public class UserMemberServiceImpl implements UserMemberService {

    private UserAccountRepository userAccountRepository;

    private UserInfoRepository userInfoRepository;

    private MemberRepository memberRepository;

    private PasswordEncoder passwordEncoder;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Long registerUserMember(RegisterReqVo reqVo) throws PermissionBusinessException {
        boolean b = userAccountRepository.existsByAccount(reqVo.mobileNumber());
        if(b){
            throw new AccountAlreadyExistsException();
        }
        UserAccount userAccount = new UserAccount();
        String mobileNumber = reqVo.mobileNumber();
        userAccount.setAccount(mobileNumber);
        userAccount.setStatus((short)1);
        userAccount.setPassword(passwordEncoder.encode(reqVo.password()));
        userAccount.setDeleted((short)0);
        userAccount.setMobileNumber(mobileNumber);
        String start = mobileNumber.substring(0, 3);
        String end = mobileNumber.substring(7);
        userAccount.setNickname("用户_"+start + "****" +end);
        userAccount.setInvitationCode(reqVo.invitationCode());
        UserAccount account = userAccountRepository.save(userAccount);
        return account.getId();
    }

    @Override
    public void resetPassword(String mobileNumber, String password) throws PermissionBusinessException {
        Optional<UserAccount> userAccountOptional = userAccountRepository.findByAccount(mobileNumber);
        if(userAccountOptional.isEmpty()){
            throw new PermissionBusinessException(ExceptionCodeEnum.ACCOUNT_NOT_FOUND);
        }
        UserAccount userAccount = userAccountOptional.get();
        userAccount.setPassword(passwordEncoder.encode(password));
    }

    @Autowired
    public void setUserAccountRepository(UserAccountRepository userAccountRepository) {
        this.userAccountRepository = userAccountRepository;
    }

    @Autowired
    public void setUserInfoRepository(UserInfoRepository userInfoRepository) {
        this.userInfoRepository = userInfoRepository;
    }

    @Autowired
    public void setMemberRepository(MemberRepository memberRepository) {
        this.memberRepository = memberRepository;
    }

    @Autowired
    public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
        this.passwordEncoder = passwordEncoder;
    }
}
